#具体的数据处理流程
import inspect
import os
import pandas as pd
import math
from function.fun_data_deal import find_elements_with_substrings, group_by_prefix, longest_common_substring, stage_divide, \
    max_hr, df_get, compar_stage_rr, compar_stage_hr
from other_function.check import check

from other_function.info import subject,file_dict



















def stage_maxhr(rrdata_df,ecg_df,test):
    '''
    #  返回每个阶段的最大心率,+每个阶段的速度+la,可以返回到一个文件夹中，main中使用循环可以保存
    :param rrdata_df:
    :param ecg_df:
    :param test: 编号
    :return:返回一个包含每个阶段最大心率，时间差，乳酸，speed的值
    example:
       stage              start_time                end_time    start_stamp      end_stamp psychology_RPE physiology_RPE      name  max_hr            diff    la speed
0       1 2024-06-21 15:28:58.380 2024-06-21 15:34:09.392  1718954938380  1718955249392              0              0      rest    79.9 00:05:11.012000  1.71
1       2 2024-06-21 15:37:27.553 2024-06-21 15:47:34.332  1718955447553  1718956054332              0              0    warmup   136.7 00:10:06.779000
2       3 2024-06-21 15:48:45.627 2024-06-21 15:54:07.256  1718956125627  1718956447256              1              1   running   113.7 00:05:21.629000  2.12     5
3       4 2024-06-21 15:54:48.240 2024-06-21 16:00:02.571  1718956488240  1718956802571              1              1   running   116.0 00:05:14.331000  1.89   6.2
4       5 2024-06-21 16:00:28.961 2024-06-21 16:05:43.298  1718956828961  1718957143298              1              1   running   129.0 00:05:14.337000  1.65   7.4
5       6 2024-06-21 16:06:27.651 2024-06-21 16:11:32.449  1718957187651  1718957492449              2              2   running   140.8 00:05:04.798000  1.64   8.6
6       7 2024-06-21 16:12:10.067 2024-06-21 16:17:26.652  1718957530067  1718957846652              4              4   running   149.9 00:05:16.585000  2.63   9.8
7       8 2024-06-21 16:18:02.024 2024-06-21 16:23:16.360  1718957882024  1718958196360              6              6   running   161.0 00:05:14.336000  2.86    11
8       9 2024-06-21 16:23:48.364 2024-06-21 16:29:00.453  1718958228364  1718958540453              8              8   running   171.1 00:05:12.089000  3.99  12.2
9      10 2024-06-21 16:30:26.343 2024-06-21 16:33:50.089  1718958626343  1718958830089              3              3     stand   124.8 00:03:23.746000
10     11 2024-06-21 16:34:43.329 2024-06-21 16:40:06.628  1718958883329  1718959206628              2              2  run_rest   104.1 00:05:23.299000
    '''


    stage_df=stage_divide(ecg_df)
    stage_maxhr=[]

    for index,stage in stage_df.iterrows():
        print(stage['start_stamp'],stage['end_stamp'])
        rrdata_df['timestamp'] = rrdata_df['timestamp'].astype('int64')

        every=rrdata_df[(rrdata_df['timestamp']>=stage['start_stamp']) & (rrdata_df['timestamp']<=stage['end_stamp'])]
        # print(every)
        stage_maxhr.append(max_hr(every))
    stage_df['max_hr']=stage_maxhr
    print(len(stage_df))
    stage_df['start_time'] = pd.to_datetime(stage_df['start_time'])
    stage_df['end_time'] = pd.to_datetime(stage_df['end_time'])
    stage_df['diff']=stage_df['end_time']-stage_df['start_time']
    la = subject[subject['id_test'] ==test].values[0][9:-1]
    la=[x for x in la if x]
    la.insert(1,'')
    if subject[subject['id_test'] == test].values[0][2] == "1":
        # 重新编写数据来源表
        speed = ['','','5', '6.2', '7.4', '8.6', '9.8', '11', '12.2', '13.4', '14.6', '15.8', '17', '18.2']
    else:
        speed = ['','','4', '5.2', '6.4', '7.6', '8.8', '10', '11.2', '12.4', '13.6', '14.8', '16', '17.2']
    # 乳酸和速度是否对应
    speed=speed[0:len(la)]
    if len(stage_df)-2 == len(la):
        print("对齐")
    else:
        print("未齐")
    for s in range(0, (len(stage_df) - len(la))): la.append('')
    for s in range(0, (len(stage_df) - len(speed))): speed.append('')

    print(la, "+", speed)
    stage_df['la']=la
    stage_df['speed']=speed
    # 按照test保存到stage文件夹
    print(stage_df)

    return  stage_df



# 检查continue rri的质量，并做与polar10 做对比,
def compar_rr(ecg_df,rrdata_df,rri_df,step):
    '''

    :param ecg_df:
    :param rrdata_df:
    :param rri_df:
    :stpe 间隔
    :return:返回对比结果
    example：[ecg的指标，ppg的指标，两者的差值占比]
        stage               start_time                 end_time    start_stamp      end_stamp psychology_RPE physiology_RPE      name                          nn50个数                                                          nn50占比                            rr个数                                                           sd1                                                           sd2                                  中位数                                                          均值                                                差值的均方根（RMSSD）                                                           标准差
0       1  2024-06-21 15:28:58.380  2024-06-21 15:34:09.392  1718954938380  1718955249392              0              0      rest   [158, 209, 27.79291553133515]     [0.6196078431372549, 0.718213058419244, 14.741168293493695]  [255, 291, 13.186813186813188]     [89.2837209841655, 184.35910330192524, 69.48867200578182]   [199.40643655828916, 288.8459876133959, 36.636602964886436]                 [992.0, 1008.0, 1.6]  [982.0549019607843, 1003.0618556701031, 2.116445154026037]   [126.02233897710295, 260.27887563812834, 69.5087313120407]    [147.8998281900019, 224.08273477167202, 40.96047189691491]
1       2  2024-06-21 15:37:27.553  2024-06-21 15:47:34.332  1718955447553  1718956054332              0              0    warmup  [104, 391, 115.95959595959596]   [0.11791383219954649, 0.7651663405088063, 146.58974990326777]  [882, 511, 53.266331658291456]   [33.385774683889274, 276.1432277349773, 156.85603039070264]   [205.11017377029563, 381.2333118848537, 60.075072862033196]    [596.0, 929.0, 43.67213114754098]   [620.5816326530612, 987.5772994129159, 45.64171605705425]    [47.1879739003197, 390.14266118745763, 156.8400014868855]   [145.99227438413064, 302.87487449658147, 69.90157355184078]
2       3  2024-06-21 15:48:45.627  2024-06-21 15:54:07.256  1718956125627  1718956447256              1              1   running     [5, 216, 190.9502262443439]   [0.010893246187363835, 0.7605633802816901, 194.3518555347838]   [459, 284, 47.10632570659489]     [10.3223799304589, 213.08598874304616, 181.5183647922441]   [43.24916149075277, 275.05313384511845, 145.65020469599008]    [568.0, 788.5, 32.51013638039071]   [570.5294117647059, 872.0845070422536, 41.80676359021041]   [14.58254624617505, 300.81712040595136, 181.5059458991007]   [31.014236516216087, 221.7623116506967, 150.92228809812397]
3       4  2024-06-21 15:54:48.240  2024-06-21 16:00:02.571  1718956488240  1718956802571              1              1   running                 [0, 195, 200.0]                                [0.0, 0.7677165354330708, 200.0]  [465, 254, 58.692628650904034]    [5.932616551948558, 222.45187785305123, 189.6094232361885]   [27.725038217823943, 312.2922766463672, 167.38396898534674]    [546.0, 876.0, 46.41350210970464]  [546.8086021505376, 921.6181102362204, 51.049126922578694]  [8.381527307120106, 313.97355191854587, 189.59963363722576]   [19.82770427654227, 247.25380182430519, 170.30463911034633]
4       5  2024-06-21 16:00:28.961  2024-06-21 16:05:43.298  1718956828961  1718957143298              1              1   running                 [0, 156, 200.0]                                [0.0, 0.6753246753246753, 200.0]   [514, 231, 75.97315436241611]   [5.113052073334441, 196.05421752277562, 189.83323264544956]      [33.90848024723266, 261.722613150223, 154.1205495571536]    [488.0, 978.0, 66.84856753069577]   [494.1011673151751, 905.9264069264069, 58.83101835823832]  [7.2254153984606235, 276.6633411708643, 189.81937081865914]   [24.11282525368276, 209.42463362557805, 158.69985848197635]
5       6  2024-06-21 16:06:27.651  2024-06-21 16:11:32.449  1718957187651  1718957492449              2              2   running                 [0, 117, 200.0]                                [0.0, 0.7697368421052632, 200.0]  [547, 152, 113.01859799713876]     [3.962780937508233, 209.8351882434535, 192.5859334348418]    [20.79400518646994, 276.5373054527646, 172.02581168896776]    [446.0, 856.0, 62.98003072196621]   [447.9616087751371, 943.7368421052631, 71.24750811017924]    [5.600235473856198, 295.7676396916027, 192.5669111602801]   [14.83648320988296, 221.91021204493768, 174.93273020108884]
6       7  2024-06-21 16:12:10.067  2024-06-21 16:17:26.652  1718957530067  1718957846652              4              4   running    [9, 182, 181.15183246073298]  [0.014634146341463415, 0.7982456140350878, 192.79886297836498]   [615, 228, 91.81494661921708]  [21.628019276052875, 247.74282340655654, 167.88365205281488]    [35.01505739514254, 289.6593316175052, 156.86132497038005]    [411.0, 778.0, 61.73254835996636]   [416.68130081300814, 904.9649122807018, 73.8902145869611]  [30.562079716740122, 349.5901565162264, 167.84227285406686]  [27.017955634057362, 239.36455634431968, 159.42983579004542]
7       8  2024-06-21 16:18:02.024  2024-06-21 16:23:16.360  1718957882024  1718958196360              6              6   running    [4, 130, 188.05970149253733]  [0.006201550387596899, 0.6310679611650486, 196.10742376644546]  [645, 206, 103.17273795534665]   [21.715458794650683, 229.2002888947996, 165.38207108223887]    [47.16584230345066, 297.9115955016833, 145.32723715181248]    [382.0, 750.0, 65.01766784452296]   [393.8325581395349, 876.0776699029126, 75.94948069782117]   [30.687009410886734, 323.363225506173, 165.33033294772363]    [35.074187485421966, 239.8100341406593, 148.9615121916563]
8       9  2024-06-21 16:23:48.364  2024-06-21 16:29:00.453  1718958228364  1718958540453              8              8   running    [12, 161, 172.2543352601156]   [0.01764705882352941, 0.5111111111111111, 186.65018541409145]   [680, 315, 73.36683417085426]   [34.718665333507396, 195.84612311369935, 139.7676192148359]    [47.79819498345808, 263.4073525184233, 138.56382655496319]    [362.0, 722.0, 66.42066420664207]   [370.8235294117647, 806.8031746031746, 74.04377698043082]   [49.06357271840241, 276.5313529637725, 139.72440127485876]   [37.99579124683891, 210.42966203710216, 138.82141987534405]
9      10  2024-06-21 16:30:26.343  2024-06-21 16:33:50.089  1718958626343  1718958830089              3              3     stand                  [0, 90, 200.0]                                [0.0, 0.6976744186046512, 200.0]    [277, 129, 72.9064039408867]   [3.7578972619481026, 180.1320696044988, 191.82576988623345]  [23.822778569176414, 252.38811896937466, 165.50059569485097]  [520.0, 1002.0, 63.337713534822605]   [519.9747292418773, 949.9612403100775, 58.50411446142959]   [5.307227776030219, 253.80575126462364, 191.8070830790803]     [16.9497149632625, 199.90440696355247, 168.7352680914541]
10     11  2024-06-21 16:34:43.329  2024-06-21 16:40:06.628  1718958883329  1718959206628              2              2  run_rest     [5, 197, 190.0990099009901]  [0.011933174224343675, 0.6118012422360248, 192.34727223034233]  [419, 322, 26.180836707152494]  [11.072515086515992, 247.17959152666654, 182.85006812649047]   [55.18301261853491, 356.60278148073337, 146.39639015304925]                  [624.0, 676.0, 8.0]   [626.6658711217184, 836.527950310559, 28.685479136784874]  [15.640932068801439, 349.0213822012125, 182.84337979907613]    [39.41107190135752, 280.8152586607791, 150.77097897331063]

    '''
    outcome=[]
    #使用test获取该stage,
    satge_df=stage_divide(ecg_df)
    for stage in satge_df.values:
        print(stage[3],stage[4])
        # 使用fun中的开始和结束的对比给出每个阶段的统计结果
        outcome.append(compar_stage_rr(rrdata_df,rri_df,start_time=stage[3],end_time=stage[4],step=step))

    # 将得到的对比结果增加到df中
    for key in sorted(outcome[0].keys()):
        satge_df[key]=[val[key] for val in outcome]
        print(key)

    print(satge_df)


    # 按照test将统计结果保存,



    return satge_df



def compar_hr(ecg_df,rrdata_df,singledetail_df,step):
    '''
    # 检查 心率的质量，并做与polar10 做对比,
    :param ecg_df:
    :param rrdata_df:
    :param singledetail_df:
    :return:
    example:
      stage               start_time                 end_time    start_stamp      end_stamp psychology_RPE physiology_RPE      name                            hr个数                                 中位数                                                            均值                                                          标准差
0       1  2024-06-21 15:28:58.380  2024-06-21 15:34:09.392  1718954938380  1718955249392              0              0      rest   [295, 62, 130.53221288515405]    [63.0, 70.0, 10.526315789473683]     [64.53559322033898, 69.40322580645162, 7.268441847525928]   [6.585744311259526, 2.3079312816101316, 96.19898960738017]
1       2  2024-06-21 15:37:27.553  2024-06-21 15:47:34.332  1718955447553  1718956054332              0              0    warmup   [605, 242, 85.71428571428571]     [92.0, 66.0, 32.91139240506329]    [94.84297520661157, 66.77272727272727, 34.737030502927574]  [21.431010622438066, 4.031257538833055, 136.67088079820448]
2       3  2024-06-21 15:48:45.627  2024-06-21 15:54:07.256  1718956125627  1718956447256              1              1   running   [321, 230, 33.03085299455535]  [105.0, 102.5, 2.4096385542168677]    [102.97196261682242, 93.86521739130434, 9.253074266906378]   [8.233837893024013, 16.500472633052265, 66.84346209135774]
3       4  2024-06-21 15:54:48.240  2024-06-21 16:00:02.571  1718956488240  1718956802571              1              1   running   [314, 302, 3.896103896103896]  [110.0, 106.0, 3.7037037037037033]  [107.13057324840764, 103.43377483443709, 3.5113241606466836]    [9.081393838356158, 9.868264429277419, 8.304852571038209]
4       5  2024-06-21 16:00:28.961  2024-06-21 16:05:43.298  1718956828961  1718957143298              1              1   running  [314, 315, 0.3179650238473768]  [123.0, 110.0, 11.158798283261802]   [119.89490445859873, 109.52698412698413, 9.038300918481875]  [8.487075655709146, 10.430620135780265, 20.547370054924652]
5       6  2024-06-21 16:06:27.651  2024-06-21 16:11:32.449  1718957187651  1718957492449              2              2   running   [304, 276, 9.655172413793103]   [134.0, 123.0, 8.560311284046692]    [131.51973684210526, 119.7572463768116, 9.362171031037873]    [8.732527407000662, 12.59158961836271, 36.19434471093921]
6       7  2024-06-21 16:12:10.067  2024-06-21 16:17:26.652  1718957530067  1718957846652              4              4   running  [316, 253, 22.144112478031637]   [146.0, 136.0, 7.092198581560284]   [141.20253164556962, 136.38735177865613, 3.469276190843537]  [11.969675702689976, 6.727708496203421, 56.071663829818505]
7       8  2024-06-21 16:18:02.024  2024-06-21 16:23:16.360  1718957882024  1718958196360              6              6   running   [314, 293, 6.919275123558484]   [157.0, 147.0, 6.578947368421052]  [150.38535031847132, 146.47440273037543, 2.6348789608083836]  [13.325175266512433, 6.6941648124230495, 66.24604435454482]
8       9  2024-06-21 16:23:48.364  2024-06-21 16:29:00.453  1718958228364  1718958540453              8              8   running  [312, 276, 12.244897959183673]     [165.0, 158.0, 4.3343653250774]  [161.13782051282053, 156.07971014492753, 3.1890484472311735]  [10.896336107709892, 9.258361601927316, 16.254022058582994]
9      10  2024-06-21 16:30:26.343  2024-06-21 16:33:50.089  1718958626343  1718958830089              3              3     stand   [204, 147, 32.47863247863248]   [115.0, 163.0, 34.53237410071942]   [115.42156862745098, 153.3673469387755, 28.234630309355246]  [4.192266571333375, 21.027080267048284, 133.50713484850272]
10     11  2024-06-21 16:34:43.329  2024-06-21 16:40:06.628  1718958883329  1718959206628              2              2  run_rest   [323, 109, 99.07407407407408]    [97.0, 104.0, 6.965174129353234]    [96.39938080495357, 128.29357798165137, 28.38913809220725]   [4.589171658351509, 33.681598105915334, 152.0347075680052]

    '''
    singledetail_df['timestamp']= singledetail_df['timestamp'].str[:13]
    outcome=[]
    #使用test获取该stage,

    satge_df=stage_divide(ecg_df)

    for stage in satge_df.values:
        print(stage[3],stage[4])
        # 使用fun中的开始和结束的对比给出每个阶段的统计结果
        outcome.append(compar_stage_hr(rrdata_df,singledetail_df,start_time=stage[3],end_time=stage[4],step=step))

    # 将得到的对比结果增加到df中
    for key in sorted(outcome[0].keys()):
        satge_df[key]=[val[key] for val in outcome]
        print(key)

    print(satge_df)


    # 按照test将统计结果保存,



    return satge_df